package com.noah.bee.admin.servlet.interceptor;

import com.noah.bee.admin.servlet.constants.GlobalConstants;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wuzhen
 * @since 16/12/19 15:32
 */

public class BeeInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object session = WebUtils.getSessionAttribute(request, GlobalConstants.USER_SESSION);
        if (null != session) return true;

        else {
            WebUtils.setSessionAttribute(request, GlobalConstants.PRE_REQUEST_URI, request.getRequestURI()); //保存当前请求的uri
            response.sendRedirect("/login");
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
